package com.qf.fmall01.service.impl;

import com.qf.fmall01.dao.*;
import com.qf.fmall01.entity.*;
import com.qf.fmall01.service.ProductService;
import com.qf.fmall01.vo.ResStatus;
import com.qf.fmall01.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductImgMapper productImgMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductParamsMapper productParamsMapper;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listRecommends() {

        ResultVO resultVO = new ResultVO();

        List<ProductVO> productVOList = productMapper.listRecommends();

        if (productVOList.size() ==0){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查询到推荐商品的数据");
        }else{
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(productVOList);
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO categoryRecommends() {

        ResultVO resultVO = new ResultVO();
        List<CategoryVO> categoryVOList = categoryMapper.categoryRecommends();
        if (categoryVOList.size() ==0){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查询商品分类推荐数据");
        }else{
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(categoryVOList);
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailInfo(Integer productId) {
        ResultVO resultVO = new ResultVO();

        Product product = productMapper.selectByPrimaryKey(productId);

        if (product == null) {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查询到该商品的信息");
        }else{
            Example example = new Example(ProductImg.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("itemId",productId);
            List<ProductImg> productImgs = productImgMapper.selectByExample(example);

            Example example1 = new Example(ProductSku.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("productId",productId);
            List<ProductSku> productSkus = productSkuMapper.selectByExample(example1);

            HashMap<String, Object> map = new HashMap<>();

            map.put("product",product);
            map.put("productImgs",productImgs);
            map.put("productSkus",productSkus);

            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(map);
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailParams(Integer productId) {

        ResultVO resultVO = new ResultVO();

        ProductParams condition = new ProductParams();
        condition.setProductId(productId+"");
        ProductParams productParams = productParamsMapper.selectOne(condition);

        if (productParams == null) {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查询到该商品的参数信息");
        }else {
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(productParams);
        }

        return resultVO;
    }
}
